package com.example.aussiehealthhelper;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class FavoriteDrugsActivity extends AppCompatActivity {

    private EditText etSearch;
    private RecyclerView rvDrugs;
    private DrugAdapter adapter;
    private HealthDbHelper dbHelper;
    private List<Drug> allFavorites = new ArrayList<>();
    private ImageButton btnBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorite_drugs);
        dbHelper = new HealthDbHelper(this);
        etSearch = findViewById(R.id.etSearch);
        rvDrugs = findViewById(R.id.rvDrugs);
        btnBack = findViewById(R.id.btnBack);
        rvDrugs.setLayoutManager(new LinearLayoutManager(this));

        adapter = new DrugAdapter(new ArrayList<>(), new DrugAdapter.DrugClickListener() {
            @Override
            public void onDrugClick(Drug drug) {
                //跳转到药品详情
                Intent intent = new Intent(FavoriteDrugsActivity.this, DrugDetailActivity.class);
                intent.putExtra("DRUG_ID", drug.getId());
                startActivity(intent);
            }
        });
        rvDrugs.setAdapter(adapter);

        //设置搜索监听
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                searchFavorites(s.toString());
            }

            @Override
            public void afterTextChanged(android.text.Editable s) {}
        });

        btnBack.setOnClickListener(v -> onBackPressed());
    }

    @Override
    protected void onResume() {
        super.onResume();
        //每次进入时刷新收藏列表
        refreshFavorites();
    }

    //刷新收藏列表
    private void refreshFavorites() {
        new Thread(() -> {
            List<Drug> favorites = dbHelper.getAllFavoriteDrugs();
            runOnUiThread(() -> {
                if (favorites != null && !favorites.isEmpty()) {
                    allFavorites = favorites;
                    searchFavorites(etSearch.getText().toString());
                } else {
                    Toast.makeText(FavoriteDrugsActivity.this,
                            "您还没有收藏任何药品",
                            Toast.LENGTH_SHORT).show();
                    // 清空列表显示
                    adapter.updateData(new ArrayList<>());
                }
            });
        }).start();
    }

    //搜索收藏的药品
    private void searchFavorites(String query) {
        List<Drug> results = new ArrayList<>();
        String keyword = query.toLowerCase().trim();

        if (keyword.isEmpty()) {
            //无搜索词时显示所有收藏药品
            results.addAll(allFavorites);
        } else {
            //有搜索词时进行过滤
            for (Drug drug : allFavorites) {
                if ((drug.getNameCN() != null && drug.getNameCN().toLowerCase().contains(keyword)) ||
                        (drug.getNameEN() != null && drug.getNameEN().toLowerCase().contains(keyword))) {
                    results.add(drug);
                }
            }
        }

        adapter.updateData(results);
    }
}